package com.sportcardio.sportcardioui.components;

import static com.sportcardio.sportcardioui.components.IComponent.TEXT_FIELD_MAX_LENGTH;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.TextField;

/**
 *
 * @author pawaptz
 */
public class LimitedTextField extends TextField implements IComponent{

    private StringProperty text;

    public LimitedTextField() {
        super();
        this.text = new SimpleStringProperty();
        this.text.bindBidirectional(this.textProperty());
        this.text.addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> ov, String t, String t1) {
                //System.out.println("ov: " + ov + "t: " + t + "t1: " + t1);
                if (ov.getValue().length() > TEXT_FIELD_MAX_LENGTH) {
                    String tmpString = ov.getValue().substring(0, TEXT_FIELD_MAX_LENGTH);
                    textProperty().setValue(tmpString);
                }

            }
        });
    }


}
